<?php	
	if(isset($_REQUEST['confirmorder'])) {
		$order = new orders();
		$orders = mysql_query("SELECT `order`.* FROM `order` WHERE `ID`=".$_REQUEST['confirmorder']);
		$order->fromArray(mysql_fetch_array($orders));
		
		// Viene verificato che l'ordine sia ancora a livello 0, quello modificabile dall'operatore in pizzeria
		if($order->getLastStatus() == 0) {		
			mysql_query("INSERT INTO `statuslog` VALUES(NULL,".$user->id.",".$_REQUEST['confirmorder'].",1,".time().",'')");

			// Viene aggiornato lo stock ingredienti disponibile, in base all'utilizzo di ingredienti per la 
			// preparazione dell'ordine appena confermato;			
			$products = mysql_query("SELECT DISTINCT `product`.* FROM `product`, `detail` WHERE `product`.`ID` = `detail`.`product_ID` AND `detail`.`order_ID`= ".$order->id);
			
			while($product_q = mysql_fetch_array($products)) {
				$product = new product();
				$product->fromArray($product_q);
				$product->updateStock($order->id);
			}
		}
	}
	
	$orders = mysql_query("SELECT `order`.* FROM `order`");
	
	if(mysql_num_rows($orders) > 0) {
		echo '<table cellspacing="0" width="100%">';
			echo '<tr>';
				echo '<th>#</th>';
				echo '<th>Data</th>';
				echo '<th>Ultimo aggiornamento</th>';
				echo '<th>Operazioni</th>';
				echo '<th>Stato</th>';
				echo '<th>Prezzo</th>';
			echo '</tr>';
			
		$i = 1;
		while($order_q = mysql_fetch_array($orders)) {
			if($i%2) {
				$class = 'al';
			} else {
				$class = 'ol';
			}
			
			$order = new orders();
			$order->fromArray($order_q);
			
			if($order->getLastStatus() == 0) {
				echo '<tr class='.$class.'>';
					echo '<td>' . $order->id . '</td>';
					echo '<td>' . date("d.m.Y H:i", $order->date) . '</td>';
					echo '<td>' . $order->getLastStatusDate() . '</td>';
					echo '<td>';
						echo '<a href="index.php?p=viewOrders&confirmorder='.$order->id.'"><button type="button"><img src="static/images/icons/add.png" /></button></a>';
						//echo '<a href="index.php?p=viewOrders&confirmorder='.$order->id.'">dafsd</a>';
					echo '</td>';
					echo '<td><b>' . $order->getLastStatusFormat() . '</b></td>';
					echo '<td width="40px"><b>' . formatPrice($order->getSumPrice()) . '</b></td>';
				echo '</tr>';
				echo '<tr class='.$class.'>';
					echo '<td colspan="4">&nbsp;</td>';
					echo '<td colspan="2">';
						echo '<table width="100%">';
						foreach($order->getProducts() as $product) {
							echo '<tr>';
								echo '<td>'.$product['quantity'].'</td>';
								echo '<td>'.$product['name'].'</td>';
								echo '<td>'.formatPrice($product['price']).'</td>';
								echo '<td width="40px">'.formatPrice(($product['price']*$product['quantity'])).'</td>';
							echo '</tr>';
							}
						echo '</table>';
					echo '</td>';
				echo '</tr>';
			
			$i++;
			}
		}
		echo '</table>';
	}
?>